package servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(name = "Handler")
public class Handler extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String username = request.getParameter("username");
        String password = request.getParameter("password");

        if("China".equals(username) && "123".equals(password)) {
            Cookie ck_name = new Cookie("username", username);
            Cookie ck_password = new Cookie("password", password);

            ck_name.setPath(request.getContextPath());
            ck_password.setPath(request.getContextPath());

            ck_name.setMaxAge(60);
            ck_password.setMaxAge(60);
            response.addCookie(ck_name);
            response.addCookie(ck_password);
            response.sendRedirect("./welcome.jsp");
        } else {
            response.sendRedirect("./errorPage.jsp");
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req, resp);
    }
}


















